import { MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/common';
import { CoffeesController } from './coffees.controller';
import { CoffeesService } from './coffees.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Coffee } from './entities/coffee.entity';
import { Flavor } from './entities/flavor.entity';
import { Event } from '../events/entities/event.entity';
import { LoggerMiddleware } from '../logger/logger.middleware';

class ConfigService {}
class DevelopmentConfigService {}
class ProductionConfigService {}

@Module({
  // 引入实体entity
  imports: [TypeOrmModule.forFeature([Coffee, Flavor, Event])],
  controllers: [CoffeesController],
  providers: [
    CoffeesService,
    {
      provide: ConfigService,
      useClass: process.env.NODE_ENV === 'development' ? DevelopmentConfigService : ProductionConfigService
    }
  ]
})
export class CoffeesModule implements NestModule {
  configure(consumer: MiddlewareConsumer): any {
    // 1. 对控制器所有路径 应用中间件
    // consumer.apply(LoggerMiddleware).forRoutes(CoffeesController)
    // 2. 只对这个控制器路由下的特定路径请求 应用中间件处理
    // consumer.apply(LoggerMiddleware).forRoutes('/v2/coffees')
    // 3. 只对这个控制器路由下并且请求方式为GET的请求 应用中间件处理
    consumer.apply(LoggerMiddleware).forRoutes({path:'/v2/coffees',method: RequestMethod.GET})
  }
}